﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using DM_Article.Model;

namespace DM_Article.Bll
{
    public class adminbll
    {
        public static int add(SqlConnection conn,admin model)
        {
            SqlCommand cmd = new SqlCommand("insert into admin(username,password,usertype)values('"+model.username+"','"+model.password+"','"+model.usertype+"')",conn);
            object obj = cmd.ExecuteNonQuery();
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public static int update(SqlConnection conn, admin model)
        {
            SqlCommand cmd = new SqlCommand("update admin set username ='" + model.username + "',password='" + model.password + "',usertype='" + model.usertype + "' where userid='" + model.userid + "'", conn);
            return cmd.ExecuteNonQuery();
        }


        public static int delete(SqlConnection conn, string userid)
        {
            SqlCommand cmd = new SqlCommand("delete admin where userid='" + userid + "'", conn);
            return cmd.ExecuteNonQuery();
        }

        public static admin getAdminDetailByName(SqlConnection conn, string name)
        {
            SqlDataAdapter da = new SqlDataAdapter("select [userid],[username],[password],[usertype] from admin where username='" + name + "'", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            admin model = new admin();
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["userid"].ToString() != "")
                {
                    model.userid = Convert.ToInt32(dt.Rows[0]["userid"].ToString());
                }
                if (dt.Rows[0]["username"].ToString() != "")
                {
                    model.username = dt.Rows[0]["username"].ToString();

                }
                if (dt.Rows[0]["password"].ToString() != "")
                {
                    model.password = dt.Rows[0]["password"].ToString();

                }
                if (dt.Rows[0]["usertype"].ToString() != "")
                {
                    model.usertype = dt.Rows[0]["usertype"].ToString();

                }
            }
            return model;
        }

        public static admin getAdminDetailById(SqlConnection conn, string id)
        {
            SqlDataAdapter da = new SqlDataAdapter("select [userid],[username],[password],[usertype] from admin where userid=" + id, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            admin model = new admin();
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["userid"].ToString() != "")
                {
                    model.userid = Convert.ToInt32(dt.Rows[0]["userid"].ToString());
                }
                if (dt.Rows[0]["username"].ToString() != "")
                {
                    model.username = dt.Rows[0]["username"].ToString();

                }
                if (dt.Rows[0]["password"].ToString() != "")
                {
                    model.password = dt.Rows[0]["password"].ToString();

                }
                if (dt.Rows[0]["usertype"].ToString() != "")
                {
                    model.usertype = dt.Rows[0]["usertype"].ToString();

                }
            }
            return model;
        }

        public static DataTable getAdminTable(SqlConnection conn)
        {
            SqlDataAdapter da = new SqlDataAdapter("select [userid],[username],[password],[usertype] from admin", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable checkAdminPassword(SqlConnection conn, string username)
        {
            SqlDataAdapter da = new SqlDataAdapter("select password from admin where username='" + username + "'", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }
}
